package org.jpass.dc.view.svn;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jpass.dc.business.project.bean.CProject;
import org.jpass.dc.business.project.logic.FileService;
import org.jpass.dc.business.project.logic.ProjectService;
import org.jpass.dc.business.svn.SVNService;
import org.jpass.dc.business.user.bean.UserVOBean;
import org.jpass.dc.common.util.StringUtil;
import org.jpass.dc.view.common.BasicAction;

import com.opensymphony.xwork2.Action;

public class SVNAction extends BasicAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -9135598459486247398L;

	private Log log = LogFactory.getLog(SVNAction.class);

	private ProjectService projectService = new ProjectService();
	private FileService fileService = new FileService();
	private SVNService svnService = new SVNService();

	private String projectName;
	private String userName;
	private String password;
	private String url;

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String checkout() {
		String msg = validateData();
		if (msg != null) {
			this.writeReturnMsg("{success:false, msg:'" + msg
					+ "', type:'warng'}");
		} else {
			UserVOBean user = this.getCurrentUser();
			CProject project = projectService.findByUserIdAndProjectName(
					user.getId(), projectName);
			if (project != null) {
				this.writeReturnMsg("{'success':false, 'msg':'Project is exist!', 'type':'error'}");
			} else {
				try {
					String projectDir = this.getUserRealWrokRootPath() + "/"
							+ projectName;
					if (!fileService.isExist(projectDir)) {
						fileService.createFolder(projectDir);
					}
					svnService.checkOut(projectDir, url, userName, password);
					CProject p = new CProject(projectName, "java", new Date(),
							user);
					projectService.create(p);
					this.writeReturnMsg("{'success':true, 'msg':'CheckOut Successfully.'}");
				} catch (Exception e) {
					e.printStackTrace();
					log.error(user.getUsername() + " check out " + projectName
							+ "项目时出现异常,", e);
					this.writeReturnMsg("{'success':false, 'msg':'System appear Exception.', 'type':'error'}");
				}

			}
		}
		
		return null;
	}
	
	public String doImport() {
		String msg = validateData();
		if (msg != null) {
			this.writeReturnMsg("{success:false, msg:'" + msg
					+ "', type:'warng'}");
		} else {
			UserVOBean user = this.getCurrentUser();
			CProject project = projectService.findByUserIdAndProjectName(
					user.getId(), projectName);
			if (project != null) {
				this.writeReturnMsg("{'success':false, 'msg':'Project is exist!', 'type':'error'}");
			} else {
				try {
					String projectDir = this.getUserRealWrokRootPath() + "/"
							+ projectName;
					if (!fileService.isExist(projectDir)) {
						fileService.createFolder(projectDir);
					}
					svnService.checkOut(projectDir, url, userName, password);
					CProject p = new CProject(projectName, "java", new Date(),
							user);
					projectService.create(p);
					this.writeReturnMsg("{'success':true, 'msg':'CheckOut Successfully.'}");
				} catch (Exception e) {
					e.printStackTrace();
					log.error(user.getUsername() + " check out " + projectName
							+ "项目时出现异常,", e);
					this.writeReturnMsg("{'success':false, 'msg':'System appear Exception.', 'type':'error'}");
				}

			}
		}
		
		return null;
	}

	private String validateData() {
		if (StringUtil.isBlank(projectName.trim())) {

			return "Project name is empty.";
		}
		if (StringUtil.isBlank(userName.trim())) {
			return "User name is empty.";
		}
		if (StringUtil.isBlank(password.trim())) {
			return "Password is empty.";
		}
		if (StringUtil.isBlank(userName.trim())) {
			return "User name is empty.";
		}
		if (StringUtil.isBlank(url.trim())) {
			return "Url is empty.";
		}
		/*if (!url.matches("^https?://.*$")) {
			return "The url must be 'http://' or 'https://' beginning.";
		}*/
		return null;
	}

	private void writeReturnMsg(String msg) {
		HttpServletResponse response = this.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		try {
			PrintWriter out = response.getWriter();
			out.write(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
